﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using BLL;

namespace WebUI.LabManage
{
    public partial class LabEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bnClose.OnClientClick = ExtAspNet.ActiveWindow.GetConfirmHideReference();
                BnClose2.OnClientClick = ExtAspNet.ActiveWindow.GetConfirmHideReference();
                InitData();
            }
        }

        private void InitData()
        {
            string action = Request.QueryString["action"];
            if (action == "new")
            {
                PageManager1.AutoSizePanelID = "newPanel";
                EditPanel.Hidden = true;
                ExtAspNet.ListItem item = new ExtAspNet.ListItem("请选择", "-1");
                dl_cat.DataSource = BLL.Parameter.GetParamList("4");
                dl_cat.DataTextField = "value";
                dl_cat.DataValueField = "id";
                dl_cat.DataBind();
                dl_cat.Items.Insert(0, item);
                dl_level.DataSource = BLL.Parameter.GetParamList("3");
                dl_level.DataTextField = "value";
                dl_level.DataValueField = "id";
                dl_level.DataBind();
                dl_level.Items.Insert(0, item);
                dl_location.DataSource = BLL.Parameter.GetParamList("10");
                dl_location.DataTextField = "value";
                dl_location.DataValueField = "id";
                dl_location.DataBind();
                dl_location.Items.Insert(0, item);
            }
            if (action == "edit")
            {
                PageManager1.AutoSizePanelID = "EditPanel";
                newPanel.Hidden = true;
                dl_edit_cat.DataSource = BLL.Parameter.GetParamList("4");
                dl_edit_cat.DataTextField = "value";
                dl_edit_cat.DataValueField = "id";
                dl_edit_cat.DataBind();
                dl_edit_level.DataSource = BLL.Parameter.GetParamList("3");
                dl_edit_level.DataTextField = "value";
                dl_edit_level.DataValueField = "id";
                dl_edit_level.DataBind();
                dl_edit_location.DataSource = BLL.Parameter.GetParamList("10");
                dl_edit_location.DataTextField = "value";
                dl_edit_location.DataValueField = "id";
                dl_edit_location.DataBind();
                int id = int.Parse(Request.QueryString["id"]);
                Model.Lab m = BLL.LabManage.SelectLabByID(id);
                tb_edit_labname.Text = m.LabName;
                tb_edit_labno.Text = m.LabNO;
                tb_edit_area.Text = m.area.ToString();
                tb_edit_summary.Text = m.summary;
                tb_edit_tel.Text = m.telephone;
                tb_edit_limituser.Text = m.limituser.ToString();
                dp_edit_useDate.SelectedDate = m.useDate;
                dl_edit_cat.SelectedValue = m.category.ToString();
                dl_edit_level.SelectedValue = m.LabLevel.ToString();
                dl_edit_location.SelectedValue = m.location.ToString();
                ck_edit_openStudent.Checked = m.openStudent;
                ck_edit_openTeacher.Checked = m.openTeacher;
            }
        }


        protected void bnSave_Click(object sender, EventArgs e)
        {
            Model.Lab m = new Model.Lab();
            m.LabName = tb_labname.Text;
            m.LabNO = tb_labno.Text;
            m.summary = tb_summary.Text;
            if (tb_area.Text != "")
            {
                m.area = int.Parse(tb_area.Text);
            }
            m.category = int.Parse(dl_cat.SelectedValue);
            m.LabLevel = int.Parse(dl_level.SelectedValue);
            m.location = int.Parse(dl_location.SelectedValue);
            m.telephone = tb_tel.Text;
            m.useDate = dp_useDate.SelectedDate.Value;
            m.limituser = int.Parse(tb_limituser.Text);
            m.openStudent = ck_openStudent.Checked;
            m.openTeacher = ck_openTeacher.Checked;
            if (BLL.LabManage.Add(m) > 0)
            {
                ExtAspNet.PageContext.RegisterStartupScript(ExtAspNet.ActiveWindow.GetHidePostBackReference(Common.LinkService.RefreshString));
            }
            else
            {
                ExtAspNet.Alert.Show("保存失败！", "失败", ExtAspNet.MessageBoxIcon.Error);
            }

        }

        protected void bnEdit_Click(object sender, EventArgs e)
        {
            Model.Lab m = new Model.Lab();
            int id = int.Parse(Request.QueryString["id"].ToString());
            m.LabID = id;
            m.LabName = tb_edit_labname.Text;
            m.LabNO = tb_edit_labno.Text;
            m.summary = tb_edit_summary.Text;
            if (tb_edit_area.Text != "")
            {
                m.area = int.Parse(tb_edit_area.Text);
            }
            m.category = int.Parse(dl_edit_cat.SelectedValue);
            m.LabLevel = int.Parse(dl_edit_level.SelectedValue);
            m.location = int.Parse(dl_edit_location.SelectedValue);
            m.telephone = tb_edit_tel.Text;
            m.useDate = dp_edit_useDate.SelectedDate.Value;
            m.limituser = int.Parse(tb_edit_limituser.Text);
            m.openStudent = ck_edit_openStudent.Checked;
            m.openTeacher = ck_edit_openTeacher.Checked;
            if (BLL.LabManage.Update(m))
            {
                ExtAspNet.PageContext.RegisterStartupScript(ExtAspNet.ActiveWindow.GetHidePostBackReference(Common.LinkService.RefreshString));
            }
            else
            {
                ExtAspNet.Alert.Show("编辑失败！", "失败", ExtAspNet.MessageBoxIcon.Error);
            }
        }
    }
}